package com.mg.plugin.agrometeorology.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;

public class ConfigReader {
    private static ConfigReader instance;
    private JSONArray validValue;
    private JSONArray riceSuit;
    private JSONArray otherSuit;
    public static ConfigReader getInstance(){
        if(instance == null){
            instance = new ConfigReader();
        }
        return instance;
    }

    private ConfigReader() {
        readTempSuitCodeConfig();
        readValidValueConfig();
    }

    private void readValidValueConfig() {
        String path = this.getClass().getClassLoader().getResource("valid-code-config.json").getPath();
        StringBuilder content = new StringBuilder();
        String jsonString = "";
        try {
            BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
            String str = reader.readLine();
            while (str != null){
                content.append(str);
                str = reader.readLine();
            }
            jsonString = content.toString();
        } catch (IOException e) {
            validValue = null;
        }
        JSONObject data = JSONObject.parseObject(jsonString).getJSONObject("Temp");
        validValue = data.getJSONArray("validValue");
    }

    /**
     * 读取风速风向编码
     */
    private void readTempSuitCodeConfig() {
        String path = this.getClass().getClassLoader().getResource("temp-suit-config.json").getPath();
        StringBuilder content = new StringBuilder();
        String jsonString = "";
        try {
            BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
            String str = reader.readLine();
            while (str != null){
                content.append(str);
                str = reader.readLine();
            }
            jsonString = content.toString();
        } catch (IOException e) {
            riceSuit = null;
            otherSuit = null;
        }
        JSONObject data = JSONObject.parseObject(jsonString).getJSONObject("Temp");
        riceSuit = data.getJSONArray("RiceSuit");
        otherSuit = data.getJSONArray("OtherSuit");
    }

    public JSONArray getTempRiceSuitCode() {
        return riceSuit;
    }

    public JSONArray getTempSuitCode() {
        return otherSuit;
    }

    public JSONArray getValidValue() {
        return validValue;
    }
}
